package cse3311.calendarapp.gui;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import cse3311.calendarapp.AddEventController;
import cse3311.calendarapp.EventData;
import cse3311.calendarapp.R;

public class FinalizeGroupEventActivity extends Activity {
	private TextView eventGroupTextView_m;
	private TextView eventNameTextView_m;
	private TextView eventDateTextView_m;
	private TextView eventAcceptanceRatioTextView_m;

	private EventData data_m;
	private String groupName_m;
	private double acceptanceRatio_m;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_finalize_group_event);

		// get the text fields from the view
		eventGroupTextView_m = (TextView) findViewById(R.id.groupNameTextView);
		eventNameTextView_m = (TextView) findViewById(R.id.eventNameTextView);
		eventDateTextView_m = (TextView) findViewById(R.id.dateAndTimeTextView);
		eventAcceptanceRatioTextView_m = (TextView) findViewById(R.id.acceptanceRatioTextView);

		// get the data from the intent
		data_m = (EventData) getIntent().getSerializableExtra("data");
		groupName_m = getIntent().getStringExtra("groupName");
		acceptanceRatio_m = getIntent().getDoubleExtra("acceptanceRatio", 1.0);

		// set group name and event name
		eventGroupTextView_m.setText(groupName_m);
		eventNameTextView_m.setText(data_m.alias_m);

		// format the time and display it
		SimpleDateFormat dateFormat = new SimpleDateFormat("E MM/dd @ hh:mm a",
				Locale.US);
		eventDateTextView_m
				.setText(dateFormat.format(data_m.start_m.getTime()));

		// format acceptance ratio and display it
		DecimalFormat decimalFormat = new DecimalFormat(".00");
		eventAcceptanceRatioTextView_m.setText(decimalFormat
				.format(acceptanceRatio_m));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.finalize_group_event, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_confirm_group_event:
			String result = AddEventController.addEvent(data_m);
			Toast toast = Toast.makeText(getApplicationContext(), result,
					Toast.LENGTH_SHORT);
			toast.show();
			finish();
			break;
		default:
			return super.onOptionsItemSelected(item);
		}

		return true;
	}
}
